package com.example.myapplication.Fragment

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.provider.MediaStore
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.example.myapplication.Model.ProfileModel
import com.example.myapplication.R
import com.google.android.material.floatingactionbutton.FloatingActionButton

class ProfileFragment : Fragment() {

    private lateinit var nameEditText: EditText
    private lateinit var emailEditText: EditText
    private lateinit var phoneEditText: EditText
    private lateinit var saveButton: Button
    private lateinit var profileImageView: ImageView
    private lateinit var sharedPreferences: SharedPreferences

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_profile, container, false)

        nameEditText = view.findViewById(R.id.name_edit_text)
        emailEditText = view.findViewById(R.id.email_edit_text)
        phoneEditText = view.findViewById(R.id.phone_edit_text)
        saveButton = view.findViewById(R.id.button_save)
        profileImageView = view.findViewById(R.id.profile_image_view)
        val fabSelectImage = view.findViewById<FloatingActionButton>(R.id.fab_select_image)

        sharedPreferences = requireActivity().getPreferences(Context.MODE_PRIVATE)

        // 加载个人资料数据，包括头像
        loadProfileData()
        loadProfileImage()

        // 设置保存按钮点击监听器
        saveButton.setOnClickListener {
            saveProfileData()
        }

        // 设置浮动操作按钮点击监听器
        fabSelectImage.setOnClickListener {
            selectProfileImage()
        }

        return view
    }

    private fun loadProfileData() {
        val name = sharedPreferences.getString("name", "John Doe")
        val email = sharedPreferences.getString("email", "john.doe@example.com")
        val phone = sharedPreferences.getString("phone", "")

        nameEditText.setText(name)
        emailEditText.setText(email)
        phoneEditText.setText(phone)
    }

    private fun loadProfileImage() {
        val imagePath = sharedPreferences.getString("profile_image_path", null)
        imagePath?.let {
            Glide.with(this)
                .load(it)
                .into(profileImageView)
        } ?: run {
            profileImageView.setImageResource(R.drawable.ic_profile_placeholder)
        }
    }

    private fun saveProfileData() {
        val name = nameEditText.text.toString()
        val email = emailEditText.text.toString()
        val phone = phoneEditText.text.toString()

        val editor = sharedPreferences.edit()
        editor.putString("name", name)
        editor.putString("email", email)
        editor.putString("phone", phone)
        editor.apply()

        Toast.makeText(requireContext(), "Profile saved", Toast.LENGTH_SHORT).show()
    }

    private fun selectProfileImage() {
        val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
        startActivityForResult(intent, REQUEST_IMAGE_PICK)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_IMAGE_PICK && resultCode == Activity.RESULT_OK && data != null) {
            val selectedImageUri = data.data
            selectedImageUri?.let {
                val editor = sharedPreferences.edit()
                editor.putString("profile_image_path", it.toString())
                editor.apply()

                Glide.with(this)
                    .load(it)
                    .into(profileImageView)
            }
        }
    }

    companion object {
        private const val REQUEST_IMAGE_PICK = 1
    }
}